﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_Them_CapNhat_NCC : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoi = null;
        public event CapNhat KhiCapNhat = null;

        public string Action = "";
        public string ID = null;

        NhaCungCap_BUS ncc_bus;
        NhaCungCap_DTO ncc_dto;
        public Form_Them_CapNhat_NCC()
        {
            InitializeComponent();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {            
            ncc_bus = new NhaCungCap_BUS();
            ncc_dto = new NhaCungCap_DTO();
            ncc_dto.Str_MaNCC = txtMaNCC.Text;
            ncc_dto.Str_TenNCC = txtNCC.Text;
            ncc_dto.Str_DiaChi = txtDiaChi.Text;
            ncc_dto.Str_DienThoai = txtDienThoai.Text;
            ncc_dto.Str_Email = txtEmail.Text;
            ncc_dto.I_TrangThai = 0;//luc them moi thi ncc_dto o trang thai chua xoa
            if (Action.Equals("Insert"))
            {
            if (ncc_bus.ThemNCC(ncc_dto) == 1)
            {
                MessageBox.Show("Thêm thành công", "Thông báo");
                if (KhiThemMoi != null)
                {
                    KhiThemMoi();
                    txtMaNCC.Text = ncc_bus.MaNCCTang();
                }
            }
            else
                MessageBoxEx.Show("Thêm thất bại", "Thông báo");
            }
            if (Action.Equals("Update"))
            {
                if (ncc_bus.CapNhatNCC(ncc_dto) == 1)
                {
                    MessageBoxEx.Show("Cập nhật thành công", "Thông báo");
                    if (KhiCapNhat != null)
                    {
                        KhiCapNhat();
                        this.Close();
                    }
                }
                else
                    MessageBoxEx.Show("Cập nhật thất bại", "Thông báo");
            }      
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form_Them_CapNhat_NCC_Load(object sender, EventArgs e)
        {
            SetBounds(Screen.GetWorkingArea(this).Width / 4, Screen.GetWorkingArea(this).Height / 4, Width, Height);
            if (Action.Equals("Insert"))
            {
                ncc_bus = new NhaCungCap_BUS();
                txtMaNCC.Text = ncc_bus.MaNCCTang().ToString();
            }
            if (Action.Equals("Update"))
            {
                ncc_bus = new NhaCungCap_BUS();
                ncc_dto = ncc_bus.DanhSachNCC_TheoMaNCC(ID);
                txtMaNCC.Text = ncc_dto.Str_MaNCC;
                txtNCC.Text = ncc_dto.Str_TenNCC;
                txtDiaChi.Text = ncc_dto.Str_DiaChi;
                txtDienThoai.Text = ncc_dto.Str_DienThoai;
                txtEmail.Text = ncc_dto.Str_Email;
            }
        }
    }
}